# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Zach Bacon <zachbacon@vba-m.com>

_realname=gpgme
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         $([[ ${CARCH} == i686 ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-qgpgme-qt5" \
           "${MINGW_PACKAGE_PREFIX}-qgpgme-qt6" \
           "${MINGW_PACKAGE_PREFIX}-python-gpgme"))
pkgver=1.23.2
pkgrel=11
pkgdesc="A C wrapper library for GnuPG (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gnupg.org/related_software/gpgme/"
msys2_repository_url="https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gpgme.git"
msys2_references=(
  'archlinux: gpgme'
  "cpe: cpe:/a:gnu:gpgme"
  "cpe: cpe:/a:gnupg:gpgme"
)
license=('spdx:LGPL-2.1-or-later OR GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gnupg"
         "${MINGW_PACKAGE_PREFIX}-libassuan"
         "${MINGW_PACKAGE_PREFIX}-libgpg-error"
         "${MINGW_PACKAGE_PREFIX}-npth")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             $([[ ${CARCH} == i686 ]] || echo \
               "${MINGW_PACKAGE_PREFIX}-python-build" \
               "${MINGW_PACKAGE_PREFIX}-python-installer" \
               "${MINGW_PACKAGE_PREFIX}-python-setuptools" \
               "${MINGW_PACKAGE_PREFIX}-qt5-base" \
               "${MINGW_PACKAGE_PREFIX}-qt6-base" \
               "${MINGW_PACKAGE_PREFIX}-swig")
             "doxygen")
source=("https://www.gnupg.org/ftp/gcrypt/${_realname}/${_realname}-${pkgver}.tar.bz2"{,.sig}
        0001-fix-find-moc-qt6.patch
        0002-Build-Python-bindings-without-distutils.patch::"https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gpgme.git;a=patch;h=5844d9e7"
        0003-Fix-build-with-setuptools-72.2.0.patch::"https://git.gnupg.org/cgi-bin/gitweb.cgi?p=gpgme.git;a=patch;h=ecd0c86d"
        0004-gpgme-find-gnupg.patch
        0005-invoke-scripts-via-sh.patch
        0007-mkdefsinc-use-CPPFLAGS.patch
        0008-no-py2.patch
        0009-fix-broken-version.patch
        0010-m4-check-python-exit-code.patch
        relocatable-cmake.patch
        gpgmepp-portable-types.patch)
#These might be signed by any of these keys https://gnupg.org/signature_key.html
validpgpkeys=('5B80C5754298F0CB55D8ED6ABCEF7E294B092E28'
              '6DAA6E64A76D2840571B4902528897B826403ADA'
              'AC8E115BF73E2D8D47FA9908E98E9B2D19C6C8BD'
              '02F38DFF731FF97CB039A1DA549E695E905BA208')
sha256sums=('9499e8b1f33cccb6815527a1bc16049d35a6198a6c5fae0185f2bd561bce5224'
            'SKIP'
            'c5079439302d931371118117ad9a2478163c7a84bb1400a89f2437459eb70f5c'
            '21cb8ff3f141427d5c55d388cb88c6f5774f592370719d3b632c4a8a70ff179b'
            '69202c576f5f9980bc88bf9e963fd6199093c89ab8dc3be02ab6c460d65fe1b4'
            '0ff731656094213854877cfe8f88f4ea562a2191c708a26f567b8ecb4873d5df'
            '75f77086c96fb4bcb9edad01bf64bbf11a7ec435f751b901f3fb180b410fa236'
            'f022993b4ae8f69f8248a6e22a0b6f0d0f43a074cdbcdbe2efb479e48064f988'
            '0b0d511f9d47844d3fd806864310d9abbeb1285d43f6ffa33babf55a4a410458'
            'ace3d5166372e0422a625f76a2890d70f2916d9239ade74e58bd23077e9c1c6b'
            '5e8e7e773a09ed1e1724c9d7216eaaecce52af49152513366026a2fa3245d762'
            '486407646276a2696e990852516afaaceca68a94a21114f8798c27c1dfe79a81'
            'a110b40d146a7c561140b68831027f61d2132e1069d2476b2e0a4034bc54fae1')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  _apply_patch_with_msg \
    0001-fix-find-moc-qt6.patch \
    0002-Build-Python-bindings-without-distutils.patch \
    0003-Fix-build-with-setuptools-72.2.0.patch \
    0004-gpgme-find-gnupg.patch \
    0005-invoke-scripts-via-sh.patch \
    0007-mkdefsinc-use-CPPFLAGS.patch \
    0008-no-py2.patch \
    0009-fix-broken-version.patch \
    0010-m4-check-python-exit-code.patch \
    relocatable-cmake.patch \
    gpgmepp-portable-types.patch

  autoreconf -ivf
}

build() {
  cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  # for clang
  export CFLAGS="${CFLAGS} -Wno-int-conversion -Wno-incompatible-pointer-types -Wno-incompatible-function-pointer-types"

  # build tool mkdefsinc uses (sn)printf '%n', which is blocked for security
  # reasons in UCRT (perhaps only recent versions).  Because it is a build tool
  # (built for the build rather than target system in the case of a
  # cross-compile), it does not respect CFLAGS, but it does respect CPPFLAGS.
  # Define __USE_MINGW_ANSI_STDIO=1 in CPPFLAGS so it uses that implementation
  # that allows '%n'.
  # See https://github.com/msys2/MINGW-packages/pull/22442#issuecomment-2463055922
  export CPPFLAGS="${CPPFLAGS} -Wp,-D__USE_MINGW_ANSI_STDIO=1"

  # mingw doxygen can't wrok with UNIX paths in doxyfile
  # --with-libtool-modification=never : Otherwise DLL name changes
  # see cmake files for the expected DLL names
  DOXYGEN=/usr/bin/doxygen \
  PYTHON=${MINGW_PREFIX}/bin/python3 \
  ./configure \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX}/bin \
    --disable-fd-passing \
    --enable-shared \
    --enable-static \
    --disable-gpgsm-test \
    --disable-gpgconf-test \
    --disable-gpg-test \
    --enable-w32-glib \
    --with-libtool-modification=never

  make

  if [[ ${CARCH} != i686 ]]; then
    # use a PEP517 workflow to get a reproducible Python package
    # NOTE: top_builddir is required so that the build takes place against local gpgme, not system gpgme
    cd lang/python/
    top_builddir="${srcdir}"/build-${MSYSTEM} \
    ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation

    cp -r "${srcdir}"/${_realname}-${pkgver} "${srcdir}"/build-${MSYSTEM}-qt6
    cd "${srcdir}"/build-${MSYSTEM}-qt6

    ./configure \
      --prefix=${MINGW_PREFIX} \
      --libexecdir=${MINGW_PREFIX}/bin \
      --disable-fd-passing \
      --enable-shared \
      --enable-static \
      --disable-gpgsm-test \
      --disable-gpgconf-test \
      --disable-gpg-test \
      --enable-w32-glib \
      --with-libtool-modification=never \
      --enable-languages=cpp,qt6

    make
  fi
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  make check || warning "Tests failed"
}

package_gpgme() {
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.LESSER" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pkgdir}${MINGW_PREFIX}/bin/gpgme-config"

  if [[ ${CARCH} != i686 ]]; then
    # split qgpgme
    rm -r "${pkgdir}${MINGW_PREFIX}"/bin/libqgpgme*
    rm -r "${pkgdir}${MINGW_PREFIX}"/lib/{cmake/QGpgme/,libqgpgme.*}
    rm -r "${pkgdir}${MINGW_PREFIX}"/lib/python*
  fi
}

package_qgpgme-qt5() {
  pkgdesc="Qt5 bindings for GPGme (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gpgme"
           "${MINGW_PACKAGE_PREFIX}-qt5-base")

  cd "${srcdir}"/build-${MSYSTEM}/lang/qt
  make DESTDIR="${pkgdir}" install
  rm -r "${pkgdir}${MINGW_PREFIX}"/include
}

package_qgpgme-qt6() {
  pkgdesc="Qt6 bindings for GPGme (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gpgme"
           "${MINGW_PACKAGE_PREFIX}-qt6-base")

  cd "${srcdir}"/build-${MSYSTEM}-qt6/lang/qt
  make DESTDIR="${pkgdir}" install
  rm -r "${pkgdir}${MINGW_PREFIX}"/include
}

package_python-gpgme() {
  pkgdesc="Python bindings for GPGme (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gpgme"
           "${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}"/build-${MSYSTEM}/lang/python

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    ${MINGW_PREFIX}/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
